(function($){
	jQuery.fn.extend({
		styledSelect: function(options, proxy, selectedIndex, callback){
			var defaults = {cls:"styled", container: "body"};
			var settings = $.extend({}, defaults, options);
			
			return this.each(function(){
				if ( this.type != 'select-one' && this.type != 'select-multiple') {
					return false;
				}
				var $this = $(this);
				$this.hide();
				
				var dfText, dfVal;
				var isSingleSelect = this.type == 'select-one';
				var $div_sel = $("<span/>");				
				var $ul_sel = $("<ul/>").addClass(settings.cls).css({display:"none",position:"absolute"});
				var $li_opts = new Array();
				var opt_index = 0;
				
				$this.children().each(function(){
					var val = this.value;
					var txt = this.text;
					var $li_opt = $("<li/>").attr("rel",val).text(txt);
					var innerIndex = opt_index;					
					$li_opt.click(function(){
						if(isSingleSelect == true){
							single_select(innerIndex, val, txt);
						}else{
							// multiple_select();
						}
					})
					$li_opts[opt_index] = $li_opt;
					$ul_sel.append($li_opts[opt_index]);
					opt_index++;
				});

				$(proxy).append($ul_sel);
				
				if(selectedIndex > 0 && selectedIndex < opt_index){
					dfText = $li_opts[selectedIndex].text();
					dfVal = $li_opts[selectedIndex].attr("rel");
				}else{
					dfText = $li_opts[0].text();
					dfVal = $li_opts[0].attr("rel");
				}
				$div_sel.attr("rel", dfVal).text(dfText);
				
				$div_sel.click(function(){
					if($ul_sel.attr("id") != "activeSelect"){
						jQuery("ul#activeSelect").removeAttr("id").hide();
						$ul_sel.attr("id", "activeSelect");
					}										
					var position = $(this).position();
					var extTop = $(this).height();
					var padBot = $(this).css('padding-bottom');
					padBot = extractValueFromPaddingValue(padBot);
					var padTop = $(this).css('padding-top');
					padTop = extractValueFromPaddingValue(padTop);
					if($ul_sel.css("display") == "none"){
						$ul_sel.css({top:position.top+extTop+padBot+padTop + 3, left:position.left}).show();
					}else{
						$ul_sel.hide();
					}
				});
				
				($this).after($div_sel);
				
				function single_select(index, val, txt){
					$this.val(val);
					$div_sel.attr("rel", val).text(txt);
					$ul_sel.hide();
					//alert("You select: " + txt + " has value: " + val + " at index: " + index);
					if(jQuery.isFunction(callback)){
						callback(index, val);
					}
				}				
				
				function extractValueFromPaddingValue(padVal){					
					var pos = padVal.lastIndexOf("px");
					if(pos > 0){
						return parseInt(padVal.substring(0, pos));
					}else{
						return 0;
					}
				}
			});
		}
	});
})(jQuery);